package com.colabo.j2ee.web.core.util;

import java.io.InputStream;
import java.text.MessageFormat;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.colabo.j2ee.web.core.exception.NoSuchFileException;

/**
 * Util class for processing archive file
 * @author Anthony Do
 * @since 2009-12-22
 *
 */
public class ArchiveUtil {

	private static Log logger = LogFactory.getLog(ArchiveUtil.class);

	private static ClassLoader getClassLoader() {
		return ArchiveUtil.class.getClassLoader();
	}

	/**
	 * get the value with the given key from the given resource file from a jar
	 * archive
	 * 
	 * @param classpath -
	 *            the relative path against the archive file, for instance:
	 *            com/colabo/j2ee/web/oaf/conf/
	 * @param filename -
	 *            file name without extension, for instance: application
	 *            (application.properties)
	 * @param subfix -
	 *            file extension,for instance: .properties
	 *            (application.properties)
	 * @param key -
	 *            key to a certain resource item
	 * @param args -
	 *            array of arguments
	 * @return value to the given key
	 */
	public static String getMessageInJar(String classPath, String fileName, String subfix, String key, Object[] args) {
		if (key == null) { // key is null
			return "";
		}
		
		String qualifiedfileName = classPath + fileName + subfix;
		logger.debug("Load file:" + qualifiedfileName);
		Properties props = new Properties();
		try {
			InputStream inCfg = getClassLoader().getResourceAsStream(qualifiedfileName);
			if (inCfg == null) { // can't find the resource file from the given place
				throw new NoSuchFileException("The file" + qualifiedfileName + "is not there!");
			}
			props.load(inCfg);
		} catch (Exception e) {
			logger.error(e);
			return key;
		}
		String value = props.getProperty(key);
		if (value == null) { // can't find the value with the given key
			return key;
		}

		if (args != null && args.length > 0) {
			//format the message with the given arguments array 
			try {
				value = MessageFormat.format(value, args);
			} catch (Exception e) {
				logger.warn("Can't format the resource!", e);
			}
		}
		return value;
	}

	/**
	 * get the value with the given key from the given resource file from a jar
	 * archive
	 * 
	 * @param classpath -
	 *            the relative path against the archive file, for instance:
	 *            com/colabo/j2ee/web/oaf/conf/
	 * @param filename -
	 *            file name without extension, for instance: application
	 *            (application.properties)
	 * @param subfix -
	 *            file extension,for instance: .properties
	 *            (application.properties)
	 * @param key -
	 *            key to a certain resource item
	 * @return value to the given key
	 */
	public static String getMessageInJar(String classpath, String filename, String subfix, String key) {
		return getMessageInJar(classpath, filename, subfix, key, null);
	}
}
